import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:test/application.dart';
import 'package:test/pages/forgetpwd/action.dart';
import 'package:test/utils/commons.dart';
import 'package:test/utils/network.dart';

import './state.dart';

Effect<ForgetPwdState> buildEffect() {
  return combineEffects(
    <Object, Effect<ForgetPwdState>>{
      Lifecycle.initState: _init,
      Lifecycle.dispose: _dispose,
      ForgetPwdAction.getValidateCode: _getValidateCode,
      ForgetPwdAction.forgetPwd: _forgetPwd
    },
  );
}

void _init(Action action, Context<ForgetPwdState> context) {}

void _dispose(Action action, Context<ForgetPwdState> context) {}

void _getValidateCode(Action action, Context<ForgetPwdState> ctx) {
  dynamic payload = action.payload;
  DioUtils.post(
    '/oauth/validatecode',
    ctx.context,
    parameters: {
      "phone": payload["phone"],
    },
  ).then((result) {
    if (result["code"] == 200) {
      showCustomNotification("获取验证码成功");
    }
  });
}

void _forgetPwd(Action action, Context<ForgetPwdState> ctx) {
  dynamic payload = action.payload;
  DioUtils.post(
    '/oauth/forgetpassword',
    ctx.context,
    parameters: {
      "phone": payload["phone"],
      "password": payload["password"],
      "validateCode": payload["validateCode"],
    },
  ).then((result) {
    if (result["code"] == 200) {
      showCustomNotification("重置密码成功");
      Navigator.of(ctx.context).pop();
    }
  });
}
